﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using HZ.MBSM.DBUtility;
public partial class Hospital_TraConfirmGoods : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    public string strConditions, Accountid_yy, strSql;
    public int projectid;

    protected void Page_Load(object sender, EventArgs e)
    {

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            Accountid_yy = SysFun.ToTrim(_Account.AccountId);
            projectid = SysFun.ToInt(Request.QueryString["projectid"]);

            Label1.Text = "";
            if (!IsPostBack)
            {

                lstDealStatus.Items.Add("全部");
                lstDealStatus.Items.Add("已入库");
                lstDealStatus.Items.Add("送货中");
                lstDealStatus.Items.Add("拒收");
                lstDealStatus.Items.Add("有退货");
                lstDealStatus.Items.Add("企业无法送货");
                lstDealStatus.SelectedValue = "送货中";

            }

            //交易状态
            if (Request.Form["btn1"] == "确认入库")
            {

                string _strDealStatus = lstDealStatus.SelectedValue;
                switch (_strDealStatus)
                {

                    case "全部":
                    case "送货中":
                        checksllect();
                        break;
                    case "已入库":
                    case "有退货":
                        break;

                }

            }

            //拒收
            if (Request.Form["btn_js"] == "拒收")
            {
                string _strDealStatus = lstDealStatus.SelectedValue;
                switch (_strDealStatus)
                {
                    case "全部":
                    case "送货中":
                        check_Rejection();
                        break;
                    case "已入库":
                    case "有退货":
                        break;
                }
            }
            showGridView();

        }

    }

    private void checksllect()
    {

        string _strBuyPlanGoodsId = "", _strGoodsId, batchnumber = "";
        DateTime validityDate;
        int _intInflowNum, intGoodsNum;
        long Receiptid;

        _strBuyPlanGoodsId = SysFun.ToTrim(Request.Form["chk"]);    //采购计划商品Id(自增)

        //是否已提交
        if (_strBuyPlanGoodsId != "")
        {

            //将“采购计划商品Id(自增)”放到数组中
            string[] _strArrayBuyPlanGoodsId = _strBuyPlanGoodsId.Length == 0 ? new string[] { "" } : _strBuyPlanGoodsId.Substring(0).Split(new char[] { ',' });

            strSql = "";

            for (int i = 0; i < _strArrayBuyPlanGoodsId.Length; i++)
            {

                _intInflowNum = SysFun.ToInt(Request.Form["InflowNum" + _strArrayBuyPlanGoodsId[i].ToString()]);
                if (Request.Form["Receiptid" + _strArrayBuyPlanGoodsId[i].ToString()] == "")
                {
                    Receiptid = 0;
                }
                else
                {
                    Receiptid = Convert.ToInt64(Request.Form["Receiptid" + _strArrayBuyPlanGoodsId[i].ToString()]);
                }
                batchnumber = Request.Form["batchnumber" + _strArrayBuyPlanGoodsId[i].ToString()].ToString();

                if (Request.Form["validityDate" + _strArrayBuyPlanGoodsId[i].ToString()].Trim() == "")
                {

                    Response.Write("<script>alert('有效期不对！')</script>");
                    continue;

                }

                validityDate = SysFun.ToDateTime(Request.Form["validityDate" + _strArrayBuyPlanGoodsId[i].ToString()].Trim());
                intGoodsNum = SysFun.ToInt(Request.Form["GoodsNum" + _strArrayBuyPlanGoodsId[i].ToString()]);
                _strGoodsId = SysFun.ToTrim(Request.Form["GoodsId" + _strArrayBuyPlanGoodsId[i].ToString()]);

                if (_intInflowNum > intGoodsNum || _intInflowNum == 0 || Receiptid == 0 || batchnumber.ToString().Length == 0 || validityDate.ToString().Length == 0)
                {

                    Response.Write("<script>alert('发票号一定要填写成数字,批号和有效期一定要填写')</script>");
                    return;

                }
                else
                {

                    strSql += "@" + "update BuyPlanGoods set DealStatus=2,InflowNumStock=" + _intInflowNum.ToString() + ",InflowNum=" + _intInflowNum.ToString() + ",HospitalInflowDate=getdate(),batchnumber='" + batchnumber +
                                    "',validityDate='" + validityDate + "',Receiptid=" + Receiptid +
                                    " where BuyPlanGoodsId=" + _strArrayBuyPlanGoodsId[i].ToString() + " and (dealstatus=1 or DealStatus=3)";

                }

            }

            if (strSql.Length > 0)
            {

                Db db = new Db();
                db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                db.Close();
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "BuyPlanGoods", strSql, "../Hospital/TraConfirmGoods.aspx");

            }

        }
        else
        {

            Response.Write("<script>alert('请选择商品！');</script>");

        }

    }

    /// <summary>
    /// 拒收
    /// </summary>
    private void check_Rejection()
    {
        string _strBuyPlanGoodsId = "", _strGoodsId, batchnumber = "";
        int intGoodsNum, Receiptid, _intInflowNum;
        DateTime validityDate;
        _strBuyPlanGoodsId = SysFun.ToTrim(Request.Form["chk"]);    //采购计划商品Id(自增)

        //是否已提交
        if (_strBuyPlanGoodsId != "")
        {

            //将“采购计划商品Id(自增)”放到数组中
            string[] _strArrayBuyPlanGoodsId = _strBuyPlanGoodsId.Length == 0 ? new string[] { "" } : _strBuyPlanGoodsId.Substring(0).Split(new char[] { ',' });

            strSql = "";

            for (int i = 0; i < _strArrayBuyPlanGoodsId.Length; i++)
            {

                _intInflowNum = SysFun.ToInt(Request.Form["InflowNum" + _strArrayBuyPlanGoodsId[i].ToString()]);
                Receiptid = SysFun.ToInt(Request.Form["Receiptid" + _strArrayBuyPlanGoodsId[i].ToString()]);
                batchnumber = Request.Form["batchnumber" + _strArrayBuyPlanGoodsId[i].ToString()].ToString();

                if (Request.Form["validityDate" + _strArrayBuyPlanGoodsId[i].ToString()].Trim() == "")
                {

                    Response.Write("<script>alert('有效期不对！')</script>");
                    continue;

                }

                validityDate = SysFun.ToDateTime(Request.Form["validityDate" + _strArrayBuyPlanGoodsId[i].ToString()].Trim());
                intGoodsNum = SysFun.ToInt(Request.Form["GoodsNum" + _strArrayBuyPlanGoodsId[i].ToString()]);
                _strGoodsId = SysFun.ToTrim(Request.Form["GoodsId" + _strArrayBuyPlanGoodsId[i].ToString()]);

                if (_intInflowNum > intGoodsNum || _intInflowNum == 0 || Receiptid == 0 || batchnumber.ToString().Length == 0 || validityDate.ToString().Length == 0)
                {

                    Response.Write("<script>alert('发票号一定要填写成数字,批号和有效期一定要填写')</script>");
                    return;

                }
                else
                {

                    strSql += "@" + "update BuyPlanGoods set DealStatus=7,InflowNumStock=" + _intInflowNum.ToString() + ",InflowNum=" + _intInflowNum.ToString() + ",HospitalInflowDate=getdate(),batchnumber='" + batchnumber +
                                    "',validityDate='" + validityDate + "',Receiptid=" + Receiptid +
                                    " where BuyPlanGoodsId=" + _strArrayBuyPlanGoodsId[i].ToString() + " and (dealstatus=1 or DealStatus=3)";

                }
            }

            if (strSql.Length > 0)
            {

                Db db = new Db();
                db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                db.Close();
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress,
                                    "更新", "BuyPlanGoods", strSql, "../Hospital/TraConfirmGoods.aspx");



            }
            else
            {

                Response.Write("<script>alert('请选择商品！');</script>");

            }
        }

    }


    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public void showGridView()
    {

        int i = 0;
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "BuyGoodsView_200708";   //要查询的表
        dbPage.Key = "BuyPlanGoodsId";          //表的主键   

        dbPage.Where = strcheck();              //查询条件

        dbPage.SelectField = "Receiptid,InflowNum,BuyPlanId,validityDate,BuyPlanGoodsId,dbo.getbackstate(BuyPlanGoodsId) as backstate,"
                            + "dbo.getbacknum(BuyPlanGoodsId) as backnum,GoodsNum,purchaseprice,Tradeprice*InflowNum as  sjprice,SupplyGoodsId,GoodsId,"
                            + "Case DealStatus When '0' Then '未送货' WHEN '1' Then '送货中' WHEN '2' then '已入库' WHEN '3' then '退货' "
                            + "when '4' then '企业无法送货' when '7' then '拒收' END AS DealStatus,"
                            + "ProductCode,batchnumber,ProductName,MedicineModel,ProjectId,CompanyName_SC,CompanyName_TB,CompanyName_PS,UserCode_PS,TradePrice,"
                            + "Case GoodsState When '0' Then '原挂网' WHEN '1' Then '备案采购商品' WHEN '2' then '不限价不竞价商品' END AS GoodsState,"
                            + "GoodsName,isnull(Outlookc,'')+'×'+convert(varchar(50),Factor)+'/'+Unit As Pack1,case QualityLevels "
                            + ConfigurationManager.AppSettings["QualityLevels"].ToString() + "  END AS QualityLevels ";     //要显示的字段

        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);                                         //要显示第几页   
        dbPage.PageSize = 20;                                                                                   //没有显示记录条数   
        dbPage.OrderBy = "DealStatus,BuyPlanId,ProductName,BuyPlanGoodsId";                                     //排序字段

        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();

        strHtml.Append("<table id='tabData' border='1'  cellpadding='3' cellspacing='0' class='list' width='100%' style='border-collapse:collapse;'>");
        strHtml.Append("<tr>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>选择</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>发票号</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>批号</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>药品有效期</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>采购单号</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>商品流水号</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>通用名</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>商品名</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>剂型</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>包装规格</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>交易价</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>采购量</th>");//InflowNum
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>退货状况</th>");//InflowNum
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>退货数</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>采购量</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>实际入库量</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>生产企业</td>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>配送企业注册号</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>配送企业</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>交易状态</th>");
        strHtml.Append("<th scope='col' style='white-space:nowrap;'>实际交易金额</th>");
        strHtml.Append("</tr>");

        System.Data.IDataReader dr = dbPage.ExecuteReader();

        while (dr.Read())
        {

            if (i == 0)
            {

                strHtml.Append("<tr class='table_td'>");
                i++;

            }
            else
            {

                strHtml.Append("<tr class='table_td2'>");
                i--;

            }

            if (dr["DealStatus"].ToString() == "已入库" || dr["DealStatus"].ToString() == "拒收" || dr["DealStatus"].ToString() == "未送货")
            {

                strHtml.Append("<td></td>");

            }
            else
            {

                strHtml.Append("<td align='center'><input name='chk' type='checkbox' value='" + dr["BuyPlanGoodsId"].ToString() + "'></td>");

            }

            if (SysFun.ToInt(dr["Receiptid"]) > 0)
            {

                strHtml.Append("<td align='center'>" + SysFun.ToTrim(dr["Receiptid"]) + "</td>");

            }
            else
            {

                strHtml.Append("<td align='center'><input type='text' size='11' maxlength='12' title='最多12位数字' name='Receiptid" + SysFun.ToTrim(dr["BuyPlanGoodsId"]) + "' value='" + SysFun.ToTrim(dr["Receiptid"]) + "' ></td>");

            }

            if (dr["batchnumber"].ToString().Length > 0)
            {

                strHtml.Append("<td align='center'>" + dr["batchnumber"].ToString() + "</td>");

            }
            else
            {

                strHtml.Append("<td align='center'><input type='text' size='10' maxlength='20' title='最多20个字符' name='batchnumber" + SysFun.ToTrim(dr["BuyPlanGoodsId"]) + "'  value='" + SysFun.ToTrim(dr["batchnumber"]) + "' ></td>");

            }
            if (dr["validityDate"].ToString().Length > 0)
            {

                strHtml.Append("<td align='center'>" + Convert.ToDateTime(dr["validityDate"]).ToString("yyyy-MM-dd") + "</td>");

            }
            else
            {

                strHtml.Append("<td align='center'><input type='text' size='7' readonly='true' name='validityDate"
                                + SysFun.ToTrim(dr["BuyPlanGoodsId"]) + "' DateTimeOnly='必须输入日期' value='"
                                + SysFun.ToTrim(dr["validityDate"]) + "' onclick='javascript:fPopCalendar(this,this);'></td>");

            }



            strHtml.Append("<td align='center'><a href='PurchaseDeal_detail.aspx?BuyPlanId=" + dr["BuyPlanId"].ToString()
                + "&projectid=" + projectid
                + "' target=_blank>"
                + dr["BuyPlanId"].ToString()
                + "</a></td>");
            strHtml.Append("<td align='center'><a href='../Bidder/GoodDetail.aspx?Goodsid=" + MsgBox.JiaMi_Encrypt(dr["GoodsId"].ToString()) + "' target=_blank>" + dr["GoodsId"].ToString() + "</a></td>");

            strHtml.Append("<td >" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td >" + dr["GoodsName"].ToString() + "</td>");
            strHtml.Append("<td align='center'>" + dr["Medicinemodel"].ToString() + "</td>");

            strHtml.Append("<td align='center'>" + dr["Pack1"].ToString() + "</td>");

            strHtml.Append("<td align='center'><input type='hidden' name='buyplangoodsid' value='" + dr["BuyPlanGoodsId"].ToString() + "'>" + SysFun.OutNum(dr["TradePrice"].ToString(), 3) + "</td>");
            strHtml.Append("<td align='center'>"
                + SysFun.ToTrim(dr["GoodsNum"])
                + "<input type='hidden' name='GoodsNum"
                + dr["BuyPlanGoodsId"].ToString() + "' value='"
                + dr["GoodsNum"].ToString() + "' /><input type='hidden' name='GoodsId"
                + dr["BuyPlanGoodsId"].ToString()
                + "' value='" + dr["GoodsId"].ToString() + "' /></td>");

            strHtml.Append("<td align='center'>" + dr["backstate"].ToString() + "</td>");

            strHtml.Append("<td align='center'><a href='ShowBackdetail.aspx?projectid=" + Request.QueryString["projectid"].ToString()
                            + "&buyplangoodsid=" + dr["BuyPlanGoodsId"].ToString()
                            + "'><font color='red'>" + dr["backnum"].ToString() + "</font></a></td>");

            strHtml.Append("<td align='center'>" + SysFun.ToTrim(dr["Goodsnum"]) + "</td>");

            //if (SysFun.ToInt(dr["InflowNum"]) > 0)
            //{

            strHtml.Append("<td align='center'><input type='hidden' size=8 name='InflowNum"
                    + SysFun.ToTrim(dr["BuyPlanGoodsId"]) + "'  value='"
                    + SysFun.ToTrim(dr["InflowNum"]) + "' >"
                    + SysFun.ToTrim(dr["InflowNum"]) + "</td>");

            //}
            //else
            //{

            //    strHtml.Append("<td><input type='text' size=8 name='InflowNum" 
            //        + SysFun.ToTrim(dr["BuyPlanGoodsId"]) + "'  value='" + SysFun.ToTrim(dr["GoodsNum"]) + "' ></td>");

            //}

            strHtml.Append("<td>" + dr["CompanyName_SC"].ToString() + "</td>");
            strHtml.Append("<td align='center'>" + dr["UserCode_PS"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_PS"].ToString() + "</td>");
            strHtml.Append("<td align='center'>" + dr["DealStatus"].ToString() + "</td>");

            strHtml.Append("<td align='center'>" + SysFun.OutNum(dr["sjprice"].ToString(), 3) + "</td>");

            strHtml.Append("</tr>");

        }

        strHtml.Append("</table>");
        Lablist.Text = strHtml.ToString();

        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    /// <summary>
    /// 查询条件
    /// </summary>
    /// <returns></returns>
    private string strcheck()
    {

        string _strTemp = "  Accountid_YY=" + Accountid_yy;
        string _strProductName, _UserCode_SC, _strCompanyName_SC, _UserCode_PS, _strCompanyName_PS, _strBuyPlanId;

        //商品流水号
        string _strGoodsId = txtGoodsId.Text.ToString().Trim();
        if (_strGoodsId != "")
        {

            _strTemp += " and GoodsId =" + _strGoodsId;

        }

        //通用名或拼音码
        _strProductName = txtProductName.Text.ToString().Trim();
        if (_strProductName != "")
        {

            _strTemp = _strTemp + " and (ProductName like '%" + _strProductName + "%' or Product_SpellingCode like '%" + _strProductName + "%')";

        }

        //生产企业名称
        _strCompanyName_SC = textCompanyName_SC.Text.ToString().Trim();
        if (_strCompanyName_SC != "")
        {

            _strTemp = _strTemp + " and CompanyName_SC like '%" + _strCompanyName_SC + "%'";

        }

        //生产企业编号
        _UserCode_SC = textCompanyName_usercoder_sc.Text.ToString().Trim();
        if (_UserCode_SC != "")
        {

            _strTemp = _strTemp + " and UserCode_SC like '%" + _UserCode_SC + "%'";

        }

        //配送企业
        _strCompanyName_PS = TextCompanyName_PS.Text.ToString().Trim();
        if (_strCompanyName_PS != "")
        {

            _strTemp = _strTemp + " and CompanyName_PS like '%" + _strCompanyName_PS + "%'";

        }

        //配送企业编号
        _UserCode_PS = textCompanyName_usercoder_ps.Text.ToString().Trim();
        if (_UserCode_PS != "")
        {

            _strTemp = _strTemp + " and UserCode_PS like '%" + _UserCode_PS + "%'";

        }

        //采购单号
        _strBuyPlanId = TextBuyPlanId.Text.ToString().Trim();
        if (_strBuyPlanId != "" && SysFun.IsNumeric(_strBuyPlanId))
        {

            _strTemp = _strTemp + " and BuyPlanId = " + _strBuyPlanId;

        }
        // '0'----'未送货' 
        // '1'----'送货中' 
        //'2' '已入库'
        // '3' ---- '退货' "
        // '4'  '企业无法送货'
        // '7' then '拒收'

        string _strDealStatus = lstDealStatus.SelectedValue;
        switch (_strDealStatus)
        {

            case "全部":
                // _strTemp = _strTemp + " and (DealStatus=1 or DealStatus=2 or DealStatus=3)";
                break;
            case "已入库":
                _strTemp = _strTemp + " and DealStatus=2";
                break;
            case "送货中":
                _strTemp = _strTemp + " and DealStatus=1";
                break;
            case "有退货":
                _strTemp = _strTemp + " and DealStatus=2 and dbo.getbacknum(BuyPlanGoodsId)>0";
                //_strTemp = _strTemp + " and DealStatus=3";
                break;
            case "企业无法送货":
                _strTemp = _strTemp + " and DealStatus=4";
                break;
            case "拒收":
                _strTemp = _strTemp + " and DealStatus=7";
                break;

        }

        return _strTemp;

    }

    #region TraDes 成员

    public string Name
    {

        get
        {

            return "入库清单列表";

        }

    }

    public string Describe
    {

        get
        {

            return "";

        }

    }

    public string ParentName
    {

        get
        {

            return "入库管理";

        }

    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {

        get
        {

            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;

        }

    }

    #endregion

    protected void Button1_Click(object sender, EventArgs e)
    {

        BindDate();

        if (this.GridView1.Rows.Count > 0)
        {

            this.GridView1.Visible = true;                          // 如果输出为Word，修改为以下代码 
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("Content-Disposition", "inline;filename=入库管理.xls");

            StringBuilder sb = new StringBuilder();
            System.IO.StringWriter sw = new System.IO.StringWriter(sb);
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);

            sb.Append("<html><body>");
            this.GridView1.RenderControl(hw);
            sb.Append("</body></html>");
            Response.Write(sb.ToString());
            Response.End();
            this.GridView1.DataSource = null;
            this.GridView1.Dispose();

        }
        else
        {

            Response.Redirect("<script>window.alert('没有信息,无法打印!')</script>");

        }

    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    public void BindDate()
    {

        string sql = "";

        if (strcheck() != "")
        {

            sql = "select BuyPlanId as '采购单号',GoodsId  as '商品流水号',ProductName as  '通用名',GoodsName as '商品名',Medicinemodel as '剂型',"
                + "isnull(Outlookc,'')+'×'+convert(varchar(50),Factor)+'/'+Unit As '包装规格',case QualityLevels "
                + ConfigurationManager.AppSettings["QualityLevels"].ToString() + "  END AS '质量层次',TradePrice as '交易价',GoodsNum as '采购量',"
                + " ISNULL(InflowNum,0) as '实际入库量',CompanyName_SC as '生产企业',UserCode_PS as '配送企业注册号',CompanyName_PS as '配送企业',"
                + " Case DealStatus When '0' Then '未送货' WHEN '1' Then '送货中' WHEN '2' then '已入库' WHEN '3' then '退货' END AS  '交易状态', "
                + " Tradeprice*InflowNum as '实际交易金额',batchnumber as '批号',validityDate as '交易的有效期' "
                + "from BuyGoodsView_200708 "
                + "where " + strcheck();

        }
        else
        {

            sql = "select BuyPlanId as '采购单号',GoodsId  as '商品流水号',ProductName as  '通用名',GoodsName as '商品名',Medicinemodel as '剂型',"
                + "isnull(Outlookc,'')+'×'+convert(varchar(50),Factor)+'/'+Unit As '包装规格',case QualityLevels "
                + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS '质量层次',TradePrice as '交易价',GoodsNum as '采购量', "
                + " ISNULL(InflowNum,0) as '实际入库量',CompanyName_SC as '生产企业',UserCode_PS as '配送企业注册号',CompanyName_PS as '配送企业',"
                + " Case DealStatus When '0' Then '未送货' WHEN '1' Then '送货中' WHEN '2' then '已入库' WHEN '3' then '退货' END AS  '交易状态', "
                + " Tradeprice*InflowNum as '实际交易金额',batchnumber as '批号',validityDate as '交易的有效期' "
                + " from BuyGoodsView_200708";

        }

        Db db = new Db();
        System.Data.IDataReader dr = db.DataReader(sql);
        this.GridView1.DataSource = dr;
        this.GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();

    }



}